<?php

namespace app\admin\controller\sys;
use app\admin\controller\Base;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;

class Attach extends Base
{
    protected $filetype;
    public function initialize()
    {
        parent::initialize(); 
        $this->filetype = [
            'image/jpeg' => '图片',
            'image/gif' => '图片',
            'image/png' => '图片',
            'image/webp' => '图片',
            'image/bmp' => '图片',
            'application/pdf' => 'pdf',
            'application/zip' => '压缩包',
            'application/msexcel' => '文档',
            'application/mspowerpoint' => '文档',
            'application/msword' => '文档',
        ];
        $mime = Db::name('attach')->field('mime')->group('mime')->select();
        foreach ($mime as $key => $value) {
            $value['mimename'] = $this->filetype[$value['mime']];
            $mime[$key] = $value;
        }
        View::assign('mimelist',$mime);
    }
    public function index(){
        $mime  = input('mime');
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit') ? $this->request->post('limit') : 1;
            $list = Db::name('attach')
                ->where('name','like',"%".$keys."%")
                ->where('mime','like',"%".$mime."%")
                ->order('id desc')
                ->paginate(['list_rows' => $limit, 'page' => $page])->each(function($item,$key){
                    $item['mime'] = $this->filetype[$item['mime']];
                    return $item;
                })->toArray();
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        return view();
    }
    /*** 选择附件*/
    public function select()
    {
        $mime  = input('mime');
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $list = Db::name('attach')
                ->where('name','like',"%".$keys."%")
                ->where('mime','like',"%".$mime."%")
                ->order('id desc')
                ->paginate(['list_rows' => $this->pageSize, 'page' => $page])->each(function($item,$key){
                    $item['mime'] = $this->filetype[$item['mime']];
                    return $item;
                })->toArray();
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        return view();
    }
    /*** 添加*/
    public function add()
    {

        return view('',['title'=>'上传','info'=>[]]);
    }
	public function state()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $id = $this->request->post('id');
            if (empty($id)) {
                $this->error('id'.lang('not exist'));
            }
            $info = Db::name('attach')->where('id',$id)->find();
            $field = $data['field'];
			$status = $info[$field]=='1'?'0':'1';
            Db::name('attach')->where('id',$id)->update([$field=>$status]);
            $this->success(lang('edit success'));
        }
    }
    public function delete($ids = "")
    {
        if ($ids) {
            $attacht = Db::name('attach')->where('id','in',$ids)->select();
            foreach ($attacht as $v) {
                 @unlink(app()->getRootPath().'public'.$v['path']);
            }
			Db::name('attach')->where('id','in',$ids)->delete();
            $this->success('删除成功');
        }
        $this->error('参数错误');
    }
}
